<?php

include_once MODELS."loja.php";
include_once MODELS."funcionario.php";

class RegiaoModel extends Database {
    
    const TABLE = "regiao";
    
    const ID = "id";
    const NOME = "nome_regiao";
    const COBRADOR = "id_funcionario_cobrador";
    const LOJA = "id_loja";
    
    public function select($fields = " * ", $condition = null, $limit = null) {
        if(is_array($fields)) $fields = implode(",", $fields);
        $res = parent::select(self::TABLE, $fields, $condition, $limit);
        $ana = $this->getAnnalisses();
        $regioes = array();
        while(($row = $ana->fetchObject($res)) !== false){
            $regioes[] = new Regiao(isset($row->{ self::ID })? $row->{ self::ID } : 0,
                                    isset($row->{ self::NOME })? ($row->{ self::NOME }) : "",
                                    isset($row->{ self::COBRADOR })? (int) $row->{ self::COBRADOR } : 0,
                                    isset($row->{ self::LOJA })? (int) $row->{ self::LOJA } : 0
                                    );
        }
        return $regioes;
    }
    
    public function superSelect($condition = null, $limit = null){
        $fields = array( self::TABLE.".".self::ID, self::NOME, 
                         FuncionarioModel::NOME, LojaModel::SIGLA );
        
        $fields_joined = implode(",", $fields);
        
        $this_condition = self::COBRADOR." = ".FuncionarioModel::TABLE.".".  FuncionarioModel::ID;
        $this_condition .= " AND ".self::TABLE.".".self::LOJA." = ".LojaModel::TABLE.".".LojaModel::ID;
        
        if(is_null($condition)) $condition = $this_condition;
        else $condition .= " AND ".$this_condition;
        
        $tables = implode(",", array(self::TABLE, FuncionarioModel::TABLE, LojaModel::TABLE));
        
        $res = parent::select($tables, $fields_joined, $condition, $limit);
        $ana = $this->getAnnalisses();
        $regioes = array();
        while(($row = $ana->fetchObject($res)) !== false){
            $regioes[] = new Regiao($row->{ self::ID },
                                    ($row->{ self::NOME }),
                                    ($row->{ FuncionarioModel::NOME }),
                                    $row->{ LojaModel::SIGLA } );
        }
        return $regioes;
    }
    
    public function insert(Regiao $regiao) {
        $fields = implode(",", array(self::NOME, self::COBRADOR, self::LOJA));
        $values = Database::turnInValues(array($regiao->nome, $regiao->cobrador, $regiao->loja));
        return parent::insert( self::TABLE, $fields, $values);
    }
    
    public function delete($condition) {
        return parent::delete(self::TABLE, $condition);
    }

    public function update(Regiao $regiao) {
        $dic = array (
            self::NOME => $regiao->nome,
            self::COBRADOR => $regiao->cobrador,
            self::LOJA => $regiao->loja
        );
        return $this->formattedUpdates( self::TABLE, 
                                        $this->turnInUpdateValues($dic), 
                                        self::ID." = ".$regiao->id);
    }
    
    public function handleError() {
        $err_code = $this->errorCode(); 
        if($err_code == Annalisses::ERR_DUPLICATE) 
            return "Já existe uma região com este nome";
        return parent::handleError();
    }
}
?>